var url = require("url");
var path = require("path");
var storeId = process.env.mpjStoreId || 'cfdtzn8vb0o4g4488gw8wko4k';

var fbsType = "LDC";
var mysqlConnOption = {
  "connectionLimit": 500,
  "database": "fbsdb",
  "host": "172.16.254.127",
  "password": "onetwo",
  "user": "root"
}
var cloudHost = "172.16.254.127"; //java服务器的地址，云端模式有效
var cloudPort = 9090; //java服务器的端口号，云端模式有效
var isHttps = true; //是否支持https，在云端模式有效
var serverRootURL = process.env.mpjServerRootURL || "http://172.16.255.162:8080"; //连接java服务器的地址，该节点在LDC模式下有效 

var defaultDataRootPath = path.normalize(process.cwd() + '/data/' + storeId);
var defaultLogRootPath = path.normalize(process.cwd() + '/logs/' + storeId);

if (process.env.fbsMyslq_connectionLimit &&
  process.env.fbsMyslq_database &&
  process.env.fbsMyslq_database &&
  process.env.fbsMyslq_host &&
  process.env.fbsMyslq_password &&
  process.env.fbsMyslq_user) {
  mysqlConnOption = {
    "connectionLimit": process.env.fbsMyslq_connectionLimit,
    "database": process.env.fbsMyslq_database,
    "host": process.env.fbsMyslq_host,
    "password": process.env.fbsMyslq_password,
  }
}
if (process.env.mpjFBSConfig_fbsType) fbsType = process.env.mpjFBSConfig_fbsType;

var urlObject = url.parse(serverRootURL);
cloudHost = urlObject.hostname;
cloudPort = urlObject.port;
if (urlObject.protocol == "https:") isHttps = true;
else isHttps = false;

var webconfig = {
    "name": fbsType + "-FBS",
    "svrType": fbsType,
    "mysqlConnOption": {
        "connectionLimit": 500,
        "database": "fbsdb",
        "host": "localhost",
        "password": "onetwo",
        "user": "root"
    },
    "cloudHost": cloudHost,
    "cloudPort": cloudPort,
    "mpjServerRootURL": serverRootURL,
    "isHttps": isHttps,
    "authWhiteList": [
        "s.menpuji.com"
    ],
    "storeId": storeId,
    "dataRootDir": defaultDataRootPath,
    "logRootDir": defaultLogRootPath,
    "dbType": "mysql"
};

if (process.env.NODE_ENV == "test") {
  webconfig.mysqlConnOption.database = "fbs_db";
}

if (fbsType == "LDC") {
  webconfig.baseService = {
    "tableService": {
      "className": "TableService",
      "src": "service/tableService"
    },
    "authService": {
      "className": "AuthService",
      "src": "service/authService"
    },
    "inventoryService": {
      "className": "InventoryService",
      "src": "service/inventoryService"
    },
    "cloudSyncService": {
      "className": "CloudSyncService",
      "src": "service/cloudSyncService"
    },
    "orderService": {
      "className": "OrderService",
      "src": "service/orderService"
    },
    "kitchenOrderService": {
      "className": "KitchenOrderService",
      "src": "service/kitchenOrderService"
    },
    "deviceService": {
      "className": "DeviceService",
      "src": "service/deviceService"
    },
    "kitchensolutionService": {
      "className": "KitchensolutionService",
      "src": "service/kitchensolutionService"
    },
    "expeditionOrderService": {
      "className": "ExpeditionOrderService",
      "src": "service/expeditionOrderService"
    },
    "deliveryOrderService": {
      "className": "DeliveryOrderService",
      "src": "service/deliveryOrderService"
    },
    "employeeService": {
      "className": "EmployeeService",
      "src": "service/employeeService"
    },
    "workshiftService": {
      "className": "WorkshiftService",
      "src": "service/workshiftService"
    },
    "frontStoreStockService": {
      "className": "FrontStoreStockService",
      "src": "service/frontStoreStockService"
    },
    "produceRecordService": {
      "className": "ProduceRecordService",
      "src": "service/produceRecordService"
    },
    "baseService": {
      "className": "BaseService",
      "src": "service/baseService"
    }
  };

  webconfig.router = [{
      "path": "/table",
      "src": "controller/tableController"
    },
    {
      "path": "/auth",
      "src": "controller/authController"
    },
    {
      "path": "/order",
      "src": "controller/orderController"
    },
    {
      "path": "/dataSync",
      "src": "controller/dataSyncController"
    },
    {
      "path": "/kitchenOrders",
      "src": "controller/kitchenOrderController"
    },
    {
      "path": "/system",
      "src": "controller/systemController"
    },
    {
      "path": "/devices",
      "src": "controller/deviceController"
    },
    {
      "path": "/kitchensolutions",
      "src": "controller/kitchensolutionController"
    },
    {
      "path": "/deliveryOrders",
      "src": "controller/deliveryOrderController"
    },
    {
      "path": "/expeditionOrders",
      "src": "controller/expeditionOrderController"
    },
    {
      "path": "/employees",
      "src": "controller/employeeController"
    },
    {
      "path": "/workshifts",
      "src": "controller/workshiftController"
    },
    {
      "path": "/frontStoreStocks",
      "src": "controller/frontStoreStockController"
    },
    {
      "path": "/scrapOutFrontStoreStockController",
      "src": "controller/scrapOutFrontStoreStockController"
    }
  ];
} else if (fbsType == "CDC") {
  webconfig.baseService = {
    "orderService": {
      "className": "WeChatOrderService",
      "src": "service/weChatOrderService"
    },
    "cloudSyncService": {
      "className": "CloudSyncService",
      "src": "service/cloudSyncService"
    },
    "weChatOrderService": {
      "className": "WeChatOrderService",
      "src": "service/weChatOrderService"
    }
  };

  webconfig.router = [{
    "path": "/order",
    "src": "controller/weChatOrderController"
  }];

  webconfig.dao = [{
    "path": "/dao",
    "src": "dao/orderDao"
  }];
}

exports.webconfig = webconfig;